package com.iiiu.payment.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.iiiu.payment.domain.entity.PaymentMethod;
import com.iiiu.payment.service.PaymentMethodService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.iiiu.payment.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 支付方式控制器
 */
@Tag(name = "支付方式管理", description = "支付方式相关API")
@RestController
@RequestMapping("/api/paymentMethod")
@Validated
@SaCheckLogin
public class PaymentMethodController {

    @Autowired
    private PaymentMethodService paymentMethodService;

    @Operation(summary = "根据用户ID查询支付方式列表", description = "获取指定用户的所有支付方式")
    @GetMapping("/user/{userId}")
    public List<PaymentMethod> getByUserId(
            @Parameter(description = "用户ID") @PathVariable @NotNull Long userId) {
        List<PaymentMethod> paymentMethods = paymentMethodService.getByUserId(userId);
        return paymentMethods;
    }

    @Operation(summary = "根据ID查询支付方式", description = "根据支付方式ID查询详细信息")
    @GetMapping("/{id}")
    public PaymentMethod getById(
            @Parameter(description = "支付方式ID") @PathVariable @NotNull Long id) {
        PaymentMethod paymentMethod = paymentMethodService.getById(id);
        
        if (paymentMethod != null && paymentMethod.getIsDelete() == 0) {
            return paymentMethod;
        }
            return null;

    }

    @Operation(summary = "创建支付方式", description = "创建新的支付方式")
    @PostMapping
    public PaymentMethod createPaymentMethod(
            @Valid @RequestBody PaymentMethod paymentMethod) {
        PaymentMethod createdMethod = paymentMethodService.createPaymentMethod(paymentMethod);
        return createdMethod;
    }

    @Operation(summary = "更新支付方式", description = "更新支付方式信息")
    @PutMapping("/{id}")
    public PaymentMethod updatePaymentMethod(
            @Parameter(description = "支付方式ID") @PathVariable @NotNull Long id,
            @Valid @RequestBody PaymentMethod paymentMethod) {
        paymentMethod.setId(id);
        PaymentMethod updatedMethod = paymentMethodService.updatePaymentMethod(paymentMethod);
        return updatedMethod;
    }

    @Operation(summary = "删除支付方式", description = "软删除支付方式")
    @DeleteMapping("/{id}")
    public boolean deletePaymentMethod(
            @Parameter(description = "支付方式ID") @PathVariable @NotNull Long id){
        boolean deleted = paymentMethodService.deletePaymentMethod(id);
        
        return deleted;
    }

    @Operation(summary = "检查支付方式名称是否存在", description = "检查指定用户下支付方式名称是否已存在")
    @GetMapping("/exists")
    public Map<String, Boolean> checkNameExists(
            @Parameter(description = "用户ID") @RequestParam @NotNull Long userId,
            @Parameter(description = "支付方式名称") @RequestParam String name) {
        boolean exists = paymentMethodService.existsByUserIdAndName(userId, name);
        return Map.of("exists", exists);
    }

    @Operation(summary = "更新支付方式排序", description = "批量更新支付方式的排序顺序")
    @PutMapping("/sort")
    public boolean updateSortOrder(
            @Parameter(description = "用户ID") @RequestParam @NotNull Long userId,
            @RequestBody List<Long> paymentMethodIds) {
        boolean updated = paymentMethodService.updateSortOrder(userId, paymentMethodIds);
        
        return updated;
    }
}